﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ConnectTheFloorDrain : HYExternalCommand
	{
		public override Result HYExecute(ref string tranName)
		{
			tranName = "连接地漏";
			ViewPlan viewPlan = base.Doc.ActiveView as Autodesk.Revit.DB.ViewPlan;
			if (viewPlan == null || (int)viewPlan.ViewType != 1)
			{
				YJKMessageBox.Show("请在楼层平面下运行该命令！");
				return Autodesk.Revit.UI.Result.Failed;
			}
			string text = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "顺水三通", base.Doc, ref text);
			bool isSSTee = false;
			if (familySymbolByName.Family.Name.Contains("顺水"))
			{
				isSSTee = true;
			}
			IList<Element> list = new List<Element>();
			ISelectionFilter selectionFilter = new PipeAndFloorDrainFilter();
			Result result;
			for (;;)
			{
				try
				{
					list = base.Sel.PickElementsByRectangle(selectionFilter, "请框选一根水管和一个地漏");
					if (list.Count == 2 && list[1].Category != list[0].Category)
					{
						Element element;
						Element elem;
						if (list[0] is Pipe)
						{
							element = list[0];
							elem = list[1];
						}
						else
						{
							element = list[1];
							elem = list[0];
						}
						Connector elementConnector = YJKRevitTools.GetElementConnector(elem);
						XYZ basisZ = elementConnector.CoordinateSystem.BasisZ;
						XYZ xyz = elementConnector.Origin + basisZ * 0.32808398950131235;
						XYZ xyz2 = elementConnector.Origin - xyz;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(element as Pipe);
						XYZ xyz3 = hypiping.StartConnector.Origin - hypiping.EndConnector.Origin;
						double num = xyz2.AngleTo(xyz3);
						Pipe pipe = YJKRevitTools.NewPipe(element as Pipe, elementConnector.Origin, xyz);
						Line line = ((element as Pipe).Location as LocationCurve).Curve as Line;
						Line line2 = (pipe.Location as LocationCurve).Curve as Line;
						XYZ xyz4 = null;
						if (!YJKRevitTools.isRiserPipe(pipe))
						{
							xyz4 = YJKLine.getNewLine(line, line2);
						}
						if ((elementConnector.Origin.Z < hypiping.GetProject(elementConnector.Origin).Z && YJKRevitTools.isRiserPipe(pipe)) || (!YJKRevitTools.isRiserPipe(pipe) && xyz4.Z > elementConnector.Origin.Z))
						{
							YJKMessageBox.Show("框选的地漏和水管位置关系不正确，不能连接");
							base.Doc.DeleteElement(pipe);
						}
						else
						{
							LocationCurve locationCurve = pipe.Location as LocationCurve;
							double num2 = elementConnector.Origin.DistanceTo(xyz);
							if (!YJKRevitTools.isRiserPipe(pipe))
							{
								double num3 = num2 * Math.Sin(0.012);
								Line curve = YJKLineEx.YJKGetBound(new XYZ(xyz.X, xyz.Y, xyz.Z - num3), elementConnector.Origin);
								locationCurve.Curve = curve;
							}
							pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(elementConnector.Radius * 2.0);
							YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe);
							if (YJKRevitTools.isRiserPipe(element as Pipe) != YJKRevitTools.isRiserPipe(pipe) || (Math.Abs(num - Math.PI) > 0.1 && num > 0.1))
							{
								YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
								string text2 = "";
								if (basisZ.Z != -1.0 && !YJKRevitTools.isRiserPipe(element as Pipe))
								{
									if (elementConnector.Origin.Z - xyz4.Z < pipe.Diameter * 2.0)
									{
										elementConnector.ConnectTo(hypiping2.GetCloseConnector(elementConnector.Origin, false));
										Location location = pipe.Location;
										if (!YJKRevitTools.isRiserPipe(pipe))
										{
											Line curve2 = YJKLineEx.YJKGetBound(xyz4, elementConnector.Origin);
											locationCurve.Curve = curve2;
										}
										hypipeConnectorHelper.TPipeConnectRPipe(pipe, element as Pipe, base.Doc, familySymbolByName, isSSTee, 2, ref text2, false);
										if (text2 != "")
										{
											YJKMessageBox.Show(text2);
											base.Doc.DeleteElement(pipe);
										}
									}
									else
									{
										YJKMessageBox.Show("框选的地漏和水管位置关系不正确，不能连接");
										base.Doc.DeleteElement(pipe);
									}
								}
								else
								{
									elementConnector.ConnectTo(hypiping2.GetCloseConnector(elementConnector.Origin, false));
									hypipeConnectorHelper.TPipeConnectRPipe(pipe, element as Pipe, base.Doc, familySymbolByName, isSSTee, 2, ref text2, false);
									if (text2 != "")
									{
										YJKMessageBox.Show(text2);
										base.Doc.DeleteElement(pipe);
									}
								}
							}
							else
							{
								YJKMessageBox.Information("框选的地漏和水管位置关系不正确，不能连接");
								base.Doc.DeleteElement(pipe);
							}
						}
					}
					else
					{
						YJKMessageBox.Information("必须框选一根水管和一个地漏");
					}
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return result;
		}
	}
}
